import copy
from typing import Optional
import typing

import PySide6.QtCore
import PySide6.QtGui
import PySide6.QtWidgets
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget


class LittleWidgetBase(QWidget):

    signalEvent = Signal(str, QWidget)
    # 类内枚举 ==========================
    EventClicked = 'clicked'
    EventTextChanged = 'textChanged'
    EventCheckChanged = 'checkChanged'
    EventComboxChanged = 'comboxChanged'
    # ==================================

    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Widget) -> None:
        ##########################################
        super().__init__(parent=parent, f=f)
        # self.setProperty()
        self._propertys = {
            # 'height':
            'enable': True,
            'height': 0,
            'width': 0,
        }

    def _UpdatePropertyValue(self, propertys: 'dict') -> None:
        '''
        function:
            获取属性
        '''
        self._propertys.update(propertys)

    def UpdateProperty(self, key: 'str', value: typing.Any) -> None:
        '''
        更新属性
        '''
        v_ = self._propertys.get(key, None)
        if (not (type(value) == type(v_))):
            return
        if (key == 'height'):
            self.resize(self.width(), value)
        elif (key == 'width'):
            self.resize(value, self.height())
        elif (key == 'enable'):
            self.setEnabled(value)

    def GetProperty(self) -> 'dict':
        '''
        function:
            获取属性
        '''
        # raise NotImplementedError
        return copy.deepcopy(self._propertys)

    def showEvent(self, event: PySide6.QtGui.QShowEvent) -> None:
        super().showEvent(event)
        self._UpdatePropertyValue({
            'height': self.height(),
            'width': self.width(),
        })

    def resizeEvent(self, event: PySide6.QtGui.QResizeEvent) -> None:
        super().resizeEvent(event)
        self._UpdatePropertyValue({
            'height': event.size().height(),
            'width': event.size().width(),
        })
